package controller

import (
	"encoding/json"
	"gobase/app/model"
	"gobase/app/service"
	"gobase/util"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

//机构控制器
type OrgController struct {
	BaseController
}

//路由注册
func (ctl *OrgController) Router(router *gin.Engine) {
	r := router.Group("admin/org")
	r.GET("/list", ctl.queryList)
	r.GET("/add", ctl.add)
	r.POST("/add", ctl.add)

	r.GET("/edit/:id", ctl.edit)
	r.PUT("/edit/:id", ctl.edit)

	r.GET("/detail/:id", ctl.detail)
	r.DELETE("/del/:id", ctl.del)
}

//列表
func (ctl *OrgController) queryList(c *gin.Context) {
	srv := service.NewOrgService(c)

	orgList := srv.QueryList()
	jsonStr, _ := json.Marshal(orgList)
	c.HTML(http.StatusOK, "org/list.html", gin.H{
		"title":    "机构列表",
		"dataJson": string(jsonStr),
		"flashmsg": ctl.GetSessionFlash(c),
	})
}

//新增
func (ctl *OrgController) add(c *gin.Context) {
	if ctl.IsPost(c.Request.Method) {
		var message = ""
		var status = false

		org := new(model.Org)
		err := c.ShouldBind(org)
		if err != nil {
			message = util.GetValidMsg(err, model.Org{})
			ctl.ResponseJSON(c, status, message)
		}

		srv := service.NewOrgService(c)
		err = srv.Add(org)
		if err != nil {
			message = err.Error()
		} else {
			status = true
		}
		ctl.ResponseJSON(c, status, message)
	} else {
		srv := service.NewOrgService(c)
		orgList := srv.GetNoDisableList()
		optionList := util.GetOrgOptionsTree("0", orgList)

		c.HTML(http.StatusOK, "org/add.html", gin.H{
			"title":         "添加机构",
			"parentOrgList": optionList,
		})
	}
}

//编辑
func (ctl *OrgController) edit(c *gin.Context) {
	idStr := c.Param("id")
	if idStr == "" {
		ctl.FlashMsg(c, "参数错误!")
		c.Redirect(http.StatusFound, "/org/list")
	}
	id, _ := strconv.Atoi(idStr)
	srv := service.NewOrgService(c)

	if ctl.IsPut(c.Request.Method) {
		var message = ""
		var status = false

		org := new(model.Org)
		err := c.ShouldBind(org)
		if err != nil {
			message = util.GetValidMsg(err, model.Org{})
			ctl.ResponseJSON(c, status, message)
		}

		err = srv.Update(id, org)
		if err != nil {
			message = err.Error()
		} else {
			status = true
		}
		ctl.ResponseJSON(c, status, message)
	} else {
		//查询机构信息
		orgInfo, _ := srv.GetByID(id)
		//父级机构下拉选择框
		orgList := srv.GetNoDisableList()
		optionList := util.GetOrgOptionsTree("0", orgList)

		c.HTML(http.StatusOK, "org/edit.html", gin.H{
			"title":         "修改机构",
			"org":           orgInfo,
			"parentOrgList": optionList,
		})
	}
}

//删除
func (ctl *OrgController) del(c *gin.Context) {
	var message = "删除失败:"
	var status = false

	idStr := c.Param("id")
	if idStr == "" {
		message += "参数为空!"
		ctl.ResponseJSON(c, status, message)
	}

	id, _ := strconv.Atoi(idStr)
	err := service.NewOrgService(c).DeleteByID(id)
	if err != nil {
		message = err.Error()
	} else {
		status = true
	}
	ctl.ResponseJSON(c, status, message)
}

//详情
func (ctl *OrgController) detail(c *gin.Context) {
	idStr := c.Param("id")
	if idStr == "" {
		ctl.FlashMsg(c, "参数错误!")
		c.Redirect(http.StatusFound, "/org/list")
	}

	id, _ := strconv.Atoi(idStr)
	srv := service.NewOrgService(c)
	//查询机构信息
	orgInfo, _ := srv.GetMapByID(id)
	c.HTML(http.StatusOK, "org/detail.html", gin.H{
		"title": "机构详情",
		"org":   orgInfo,
	})
}
